﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using Sunboxsoft.Common;
using System.Windows.Forms;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.ASMS.Client.UI.Properties;
namespace Sunboxsoft.ASMS.Client.UI.Show
{
    /// <summary>
    /// 查看客户详细信息
    /// </summary>
    public partial class ShowCustomerDetailView : UserControl, IView
    {
        Guid _customerId;
        Promise _promise = new Promise();
        public ShowCustomerDetailView()
        {
            InitializeComponent();
        }
        public ShowCustomerDetailView(Guid customerId)
            : this()
        {
            this._customerId = customerId;
        }

        #region IView 成员
        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("close", Resources.isDelete, "关闭", OnCloseClick));
        }
        public void Resume(ViewResumeArgs args)
        {
        }
        public void Active(ViewActiveArgs args)
        {
            _promise.Then(GetVCustomer)
            .Run(null);
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("查看客户详细信息");
        }
        #endregion

        #region Method
        private void GetVCustomer()
        {
            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                SingleResult<WebRef.VCustomer> result = new Service.GetService().GetCustomer(_customerId);
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        this.ctlEditTelephone.Text = result.Data.Telephone;
                        this.ctlEditName.Text = result.Data.Name;
                        this.ctlEditIdNumber.Text = result.Data.IdNumber;
                        this.ctlEditCustomerType.Text = result.Data.TypeName;
                        this.ctlEditGender.Text = result.Data.GenderText;
                        if (result.Data.IsCredit)
                        {
                            this.ctlEditIsTrust.Text = "是";
                        }
                        else
                        {
                            this.ctlEditIsTrust.Text = "否";
                        }
                        this.ctlEditSalesManager.Text = result.Data.SalesManagerName;
                        this.ctlEditCountry.Text = result.Data.CountryName;
                        this.ctlEditProvince.Text = result.Data.ProvinceName;
                        this.ctlEditCity.Text = result.Data.CityName;
                        this.ctlEditDistrict.Text = result.Data.DistrictName;
                        this.ctlEditAddress.Text = result.Data.Address;
                        this.ctlEditBrith.Text = result.Data.Birthday.ToString();
                        ctlEditVIPNumber.Text = result.Data.VIPNumber;
                        ctlEditVIPLevel.Text = result.Data.VIPLevelText;
                        txtEMail.Text = result.Data.EMail;
                        txtQQ.Text = result.Data.QQ;
                        txtWebChat.Text = result.Data.Wechat;
                        _promise.Resolve();
                    }
                    else
                    {
                        MessageBox.Show(result.Message);
                        _promise.Reject();
                    }
                    this.HideMask();
                }));
            });
        }
        #endregion

        #region Action
        /// <summary>
        /// 关闭
        /// </summary>
        private void OnCloseClick(object sender, object e)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }
        #endregion
    }
}
